/**
 * Javascript for PPS new design
 * using jQuery v1.3.2
 * @author Yukwunhang
 */

/**
 * Function regarding the displaying of the login box.
 * Hide, fade in and fade out.
 */
function loginBox()
{
	$('div.login').hide(0);
	$('.login_btn').click(function(){
		if ( $('div.login').is(':hidden') )
		{
			$('div.login').fadeIn('slow');
			$(this).addClass('active');
			return false;
		}
		else
		{
			$('div.login').fadeOut('slow');
			$(this).removeClass('active');
			return false;
		}
	});
}

/***************************************************************************************/

/**
 * Clear the search box text when focused on.
 * Repopulate 'Search..' into the text box when it's blank when blurred.
 */
function searchClear()
{
	var text = 'Search..';
	$('#search_input').focus(function(){
		if ($(this).val() == text)
		{
			$(this).val('');
		}
	});
	$('#search_input').blur(function(){
		if ($(this).val() == '')
		{
			$(this).val(text);
		}
	});
}

/***************************************************************************************/

/**
 * Read the raw cookie information.
 * @param {Object} cookieName
 */
function readCookie(cookieName)
{
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

/**
 * Display overlaying 'modal-like' messages
 * using NMS
 * 
 * @param {Object} type success = 0, error = 1, warning = 2
 * @param {Object} msg Message displayed
 * @param {Object} find regex
 */
function displayMessage(type, msg, find)
{
	var flash = readCookie('ci_session');
	var success = [];
		success['text_color'] = '#264409';
		success['border_color'] = '#C6D880';
		success['background'] = '#E6EFC2';
		success['icon'] = 'success.png';
	var error = [];
		error['text_color'] = '#8a1f11';
		error['border_color'] = '#FBC2C4';
		error['background'] = '#FBE3E4';
		error['icon'] = 'error.png';
	var msgvar = [success, error];
	if (flash)
	{
		if (flash.search(find) != -1) {
			$('body').nms_msg({
				text: msg,
				text_color: msgvar[type]['text_color'],
				text_align: 'center',
				border_color: msgvar[type]['border_color'],
				background: msgvar[type]['background'],
				freeze: true,
				close: true,
				icon: msgvar[type]['icon'],
				icon_path: 'assets/images/',
			});
		}
	}
}

/**
 * Wrapper for displayMessage
 */
function modalMessageBox()
{
	displayMessage(1, '<strong>Error:</strong> Incorrect username and/or password.', /"flash:old:login_fail";s:1:"1"/);
	displayMessage(0, 'You have been successfully logged out.', /"flash:old:logout_success";s:1:"1"/);
	displayMessage(0, 'Your IP address has been successfully updated.', /"flash:old:update_ip";s:1:"1"/);
}

/***************************************************************************************/

/**
 * Tooltips using qTip
 */
function showTooltips()
{
	/* Server info header */
	$('#tier1').qtip({
		content: '<strong>IP:</strong> fracs.net <strong>Port:</strong> 20202<br /><br />Click here to join!',
		style:
		{
			tip: true,
			padding: 6,
			'font-size': 16,
			color: '#fafafa',
			background: '#98aab1',
			border:
			{
				width: 3,
				radius: 5,
				color: '#333',
				color: '#98aab1'
			},
		},
		position:
		{
			target: $('.table h2 a'),
			corner:
			{
				target: 'leftMiddle',
				tooltip: 'rightMiddle',
			},
			adjust:
			{
				x: -8,
			},
		},
	});
	
	/* Dev feeds */
	$('a[rel="feeds"]').qtip({
      content: {
         title: {
            text: 'Developement Updates',
            button: 'Close',
         },
		 url: 'http://' + location.host + '/pps/index.php/ajax/feeds',
      },
      position: {
         target: $(document.body), // Position it via the document body...
         corner: 'center' // ...at the center of the viewport
      },
      show: {
         when: 'click', // Show it on click
         solo: true // And hide all other tooltips
      },
      hide: false,
      style: {
         width: { max: 750, min: 750 },
		 height: { max: 750, },
         padding: '14px',
         border: {
            width: 9,
            radius: 9,
            color: '#666666'
         },
		 background: '#fafafa',
         name: 'light',
      },
      api: {
         beforeShow: function()
         {
            // Fade in the modal "blanket" using the defined show speed
            $('#qtip-blanket').fadeIn(this.options.show.effect.length);
         },
         beforeHide: function()
         {
            // Fade out the modal "blanket" using the defined hide speed
            $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
         }
      }
   });

   // Create the modal backdrop on document load so all modal tooltips can use it
   $('<div id="qtip-blanket">')
      .css({
         position: 'absolute',
         top: $(document).scrollTop(), // Use document scrollTop so it's on-screen even if the window is scrolled
         left: 0,
         height: $(document).height(), // Span the full document height...
         width: '100%', // ...and full width

         opacity: 0.7, // Make it slightly transparent
         backgroundColor: 'black',
         zIndex: 5000  // Make sure the zIndex is below 6000 to keep it below tooltips!
      })
      .appendTo(document.body) // Append to the document body
      .hide() // Hide it initially
}
/***************************************************************************************/
/**
 * Main function; jQuery on document ready.
 */
$(function(){
	loginBox();
	searchClear();
	modalMessageBox();
	showTooltips();
});